package com.lgx.authorize.module.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Component;

import com.lgx.authorize.module.dao.IAuthorizeUserInfoDao;
import com.lgx.authorize.module.entity.ApplicationApplykeyInfo;
import com.lgx.authorize.module.entity.AuthorizeUserInfo;
import com.lgx.authorize.util.DateCharger;
import com.lgx.authorize.util.RowMapperEffective;

@Component("authorizeUserInfoDao")
public class AuthorizeUserInfoDao implements IAuthorizeUserInfoDao {

	@Resource
	private JdbcTemplate jdbcTemplate_sqlite;
	
	/**
	 * this is getter
	 * @return
	 */
	public JdbcTemplate jdbcTemplate_sqlite() {
		return jdbcTemplate_sqlite;
	}

	@Override
	public void createAuthorizeUserInfo(final AuthorizeUserInfo authorizeUserInfo) {
		final String sql = "insert into authorizeuserinfo(startTime,endTime,authorizeToken,appkeyId,userId)" +
				" values(?,?,?,?,?)";
		jdbcTemplate_sqlite().execute(sql, new PreparedStatementCallback<AuthorizeUserInfo>() {
			@Override
			public AuthorizeUserInfo doInPreparedStatement(
					PreparedStatement ps) throws SQLException,
					DataAccessException {
				ps.setString(1,DateCharger.chargeTime(authorizeUserInfo.getStartTime()));
				ps.setString(2, DateCharger.chargeTime(authorizeUserInfo.getEndTime()));
				ps.setString(3, authorizeUserInfo.getAuthorizeToken());
				ps.setInt(4, authorizeUserInfo.getAppkeyId());
				ps.setString(5, String.valueOf(authorizeUserInfo.getUserId()));
				ps.execute();
				return authorizeUserInfo;
			}
		});
	}

	@Override
	public void updateAuthorizeUserInfo(final AuthorizeUserInfo authorizeUserInfo) {
		final String sql = "update authorizeuserinfo set startTime=?," +
				"endTime=?,authorizeToken=?,appkeyId=?,userId=?" +
				" where systemid=?";
		jdbcTemplate_sqlite().execute(sql, new PreparedStatementCallback<AuthorizeUserInfo>() {

			@Override
			public AuthorizeUserInfo doInPreparedStatement(PreparedStatement ps)
					throws SQLException, DataAccessException {
				ps.setString(1,DateCharger.chargeTime(authorizeUserInfo.getStartTime()));
				ps.setString(2, DateCharger.chargeTime(authorizeUserInfo.getEndTime()));
				ps.setString(3, authorizeUserInfo.getAuthorizeToken());
				ps.setInt(4, authorizeUserInfo.getAppkeyId());
				ps.setString(5, String.valueOf(authorizeUserInfo.getUserId()));
				ps.setInt(6, authorizeUserInfo.getSystemid());
				ps.execute();
				return authorizeUserInfo;
			}
		});
	}

	@Override
	public void deleteAuthorizeUserInfo(final AuthorizeUserInfo authorizeUserInfo) {
		final String sql = "delete from authorizeuserinfo where systemid=?";
		jdbcTemplate_sqlite().execute(sql, new PreparedStatementCallback<AuthorizeUserInfo>() {
			@Override
			public AuthorizeUserInfo doInPreparedStatement(PreparedStatement ps)
					throws SQLException, DataAccessException {
				ps.setInt(1, authorizeUserInfo.getSystemid());
				ps.execute();
				return authorizeUserInfo;
			}
		});
	}

	@Override
	public AuthorizeUserInfo findAuthorizeUserInfoById(int systemid) {
		String sql = "select * from authorizeuserinfo where systemid=?";
		Object[] args = new Object[]{systemid};
		List<AuthorizeUserInfo> list = jdbcTemplate_sqlite().query(sql, args,
				new RowMapperEffective<AuthorizeUserInfo>(AuthorizeUserInfo.class));
		return list==null?null:list.get(0);
	}

	@Override
	public AuthorizeUserInfo findAuthorizeUserInfoByUser(String userId) {
		String sql = "select * from authorizeuserinfo where userId=?";
		Object[] args = new Object[]{userId};
		List<AuthorizeUserInfo> list = (List<AuthorizeUserInfo>) jdbcTemplate_sqlite().query(sql, args,
				new RowMapperEffective<AuthorizeUserInfo>(AuthorizeUserInfo.class));
		return list==null?null:list.get(0);
	}

	@Override
	public List<AuthorizeUserInfo> listAllAuthorizeUserInfos() {
		String sql = "select * from authorizeuserinfo";
		return jdbcTemplate_sqlite().query(sql, new Object[]{}, new RowMapperEffective<AuthorizeUserInfo>(AuthorizeUserInfo.class));
	}

	@Override
	public List<AuthorizeUserInfo> listAuthorizeUserInfosByAppKey(
			ApplicationApplykeyInfo applicationApplykeyInfo) {
		String sql = "select * from authorizeuserinfo where appkeyId=?";
		Object[] args = new Object[]{applicationApplykeyInfo.getSystemid()};
		return jdbcTemplate_sqlite().query(sql, args,new RowMapperEffective<AuthorizeUserInfo>(AuthorizeUserInfo.class));
	}
	
	

}
